package com.ymx.darling.wrapper;

import com.ymx.darling.bean.entity.TableAlias;
import com.ymx.darling.wrapper.fragment.SqlSegment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * wrapper工具类
 * @author 爱Java的小于
 */
public class Wrappers {
    /**
     * 快速构建QueryWrapper
     * @return queryWrapper
     */
    public static QueryWrapper query() {
        return new QueryWrapper();
    }


    /**
     * 快速构建UpdateWrapper
     * @return updateWrapper
     */
    public static UpdateWrapper update() {
        return new UpdateWrapper();
    }


    /**
     * 返回queryWrapper的from的表
     * @param queryWrapper qw
     * @return list
     */
    public static List<String> getQueryFroms(QueryWrapper queryWrapper) {
        return queryWrapper.getQueryFroms();
    }


    /**
     * 返回queryWrapper的join的表
     * @param queryWrapper qw
     * @return list
     */
    public static List<String> getJoinFroms(QueryWrapper queryWrapper) {
        return queryWrapper.getJoinFroms();
    }


    /**
     * 返回queryWrapper中所有的表及其别名
     * @param queryWrapper qw
     * @return list
     */
    public static List<TableAlias> getQueryTables(QueryWrapper queryWrapper) {
        List<TableAlias> tableAliases = new ArrayList<>();
        List<String> queryFroms = queryWrapper.getQueryFroms();
        for (String queryFrom : queryFroms) {
            TableAlias tableAlias = TableAlias.splitTableAndAlias(queryFrom);
            tableAliases.add(tableAlias);
        }
        return tableAliases;
    }


    /**
     * 返回queryWrapper中内嵌的子查询queryWrapper
     * @param queryWrapper qw
     * @return list
     */
    public static List<QueryWrapper> getNestedQueryWrappers(QueryWrapper queryWrapper) {
        return queryWrapper.getNestedQueryWrappers();
    }


    /**
     * 将queryWrapper以分段sql的形式返回
     * @param queryWrapper qw
     * @return map
     */
    public static Map<String, SqlSegment> getSqlSegmentMap(Wrapper<?> queryWrapper) {
        return queryWrapper.getSqlSegmentMap();
    }


    /**
     * 返回queryWrapper的limit参数
     * @param queryWrapper qw
     * @return Long
     */
    public static Long getLimit(QueryWrapper queryWrapper) {
        return queryWrapper.getLimit();
    }


    /**
     * 返回queryWrapper的offset参数
     * @param queryWrapper aw
     * @return Long
     */
    public static Long getOffset(QueryWrapper queryWrapper) {
        return queryWrapper.getOffset();
    }
}
